package cn.com.wxd.util.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.entity.user.AccountInfo;
import cn.com.wxd.entity.user.UsersInfo;
import cn.com.wxd.service.address.AddressBean;
import cn.com.wxd.service.address.AddressService;
import cn.com.wxd.service.weather.WeatherService;
import cn.com.wxd.service.weather.Weatherbean;
import cn.com.wxd.util.user.UserCoreUtil;

/**
 * web常用工具类
 *
 * @author wangxudong
 */
public class WebUtil {
    private static final Logger log = LoggerFactory.getLogger(WebUtil.class);

    /**
     * 获取目前的天气状况
     *
     * @param request 用户请求request
     *
     * @return 天气对象
     */
    public static Weatherbean getWeather(HttpServletRequest request) {
        String province = "北京";
        try {
            AddressService as = new AddressService();
            AddressBean ab;
            ab = as.getAddress(request, "UTF-8");
            if (ab != null) {
                province = ab.getProvince();
            } else {
                log.debug("请求IP无法获取到地址");
            }
        } catch (Exception e) {
            log.error("请求IP无法查到相关地址" + e.getMessage(), e);
        }
        if (province != null && !province.equals("")) {
            try {
                WeatherService ws = new WeatherService();
                return ws.getWeather(province);
            } catch (Exception e) {
                log.error("天气服务暂时无法获取信息" + e.getMessage(), e);
            }
        }
        return null;
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate() {
        Date dt = new Date();
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    /**
     * 获取当前时间的时间段 如上午，下午等
     *
     * @return
     */
    public static String getHelloTime() {
        Calendar c = Calendar.getInstance();
        int hours = c.get(Calendar.HOUR_OF_DAY);
        if (hours < 4) {
            return "凌晨";
        }
        if (hours < 8) {
            return "早上";
        }
        if (hours < 11) {
            return "上午";
        }
        if (hours < 14) {
            return "中午";
        }
        if (hours < 18) {
            return "下午";
        }
        if (hours < 24) {
            return "晚上";
        }
        return "";

    }

    /**
     * 获取今天，昨天，前天，小时前，分钟前，刚刚，根据时间点获取，如果大于前天或无法识别，则返回原字符串
     *
     * @param dateTime 时间字符串 字符串格式 必须为yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getDateintervalExt(String dateTime) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now;
        try {
            now = new Date();
            Date date = df.parse(dateTime);
            long l = now.getTime() - date.getTime();
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            //long s=(l/1000-day*24*60*60-hour*60*60-min*60);
            if (day > 0) {
                return getDateinterval(dateTime, "yyyy-MM-dd HH:mm:ss");
            }
            if (hour > 0) {
                return hour + "小时前";
            }
            if (min > 0) {
                return min + "分钟前";
            }
            return "刚刚";
            //return s+"秒前";
        } catch (ParseException e) {
            log.warn(e.getMessage());
            return dateTime;
        }
    }

    /**
     * 获取今天，昨天，前天字符，根据时间点获取，如果大于或者无法识别，则返回原字符串
     *
     * @param createTime 时间字符串
     * @param dateFormat 字符串格式 默认为yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getDateinterval(String createTime, String dateFormat) {
        try {
            String ret = "";
            if (dateFormat == null || "".equals(dateFormat.trim())) {
                return createTime;
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long create = sdf.parse(createTime).getTime();
            Calendar now = Calendar.getInstance();
            long ms = 1000 * (now.get(Calendar.HOUR_OF_DAY) * 3600 + now.get(Calendar.MINUTE) * 60 + now
                    .get(Calendar.SECOND));//毫秒数
            long ms_now = now.getTimeInMillis();
            if (ms_now - create < ms) {
                ret = "今天";
            } else if (ms_now - create < (ms + 24 * 3600 * 1000)) {
                ret = "昨天";
            } else if (ms_now - create < (ms + 24 * 3600 * 1000 * 2)) {
                ret = "前天";
            }
            if (!ret.equals("")) {
                return ret;
            }
        } catch (Exception e) {
            log.warn(e.getMessage());
        }
        return createTime;
    }

    /**
     * 获取用户头像地址,如果用户没有头像，则返回默认头像
     *
     * @param requestContextPath 请求上下文路径 request.getContentPath()返回的值
     * @param usersInfo          用户对象
     *
     * @return 用户头像地址
     */
    public static String getUserHeadImg(String requestContextPath, UsersInfo usersInfo) {
        if (usersInfo == null) {
            return requestContextPath + "/static/images/defaultHeadImg.png";
        }
        if (usersInfo.getUserType().equalsIgnoreCase("ordinary")
                || usersInfo.getUserType().equalsIgnoreCase("admin")) {
            String uhi = usersInfo.getHeadImg();
            if (uhi == null || uhi.equals("")) {
                uhi = requestContextPath + "/static/images/defaultHeadImg.png";
                return uhi;
            }
            return requestContextPath + "/Stream/userheadpic/" + uhi;
        } else {
            String uhi = usersInfo.getHeadImg();
            if (uhi == null || uhi.equals("")) {
                uhi = requestContextPath + "/static/images/defaultHeadImg.png";
            }
            return uhi;
        }
    }

    /**
     * 获取当前登录的用户头像地址,如果用户没有头像，则返回默认头像
     *
     * @param request
     *
     * @return 用户头像地址
     */
    public static String getUserHeadImg(HttpServletRequest request) {
        AccountInfo accountInfo = UserCoreUtil.getLoginAccount(request);
        String requestContextPath = request.getContextPath();
        UsersInfo usersInfo = null;
        if (accountInfo != null) {
            usersInfo = accountInfo.getUsersInfo();
        }
        return getUserHeadImg(requestContextPath, usersInfo);
    }
}
